package com.zlq.login.servlet;

import com.google.code.kaptcha.Constants;
import com.google.gson.JsonObject;
import com.zlq.login.dao.UserDaoLo;
import com.zlq.login.entity.User;
import net.sf.json.JSONObject;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@WebServlet(urlPatterns = "/loginServlet")
public class LoginServlet extends HttpServlet {

    private UserDaoLo ud = new UserDaoLo();


    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=Utf-8");

        String na = req.getParameter("na");
        User us = new User();
        us.setName(na);
        UserDaoLo ud = new UserDaoLo();
        List<User> users = ud.selectName(us);
        JSONObject json = new JSONObject();
        if (users != null){
            json.put("result",false);
        }else {
            json.put("result",true);
        }
        PrintWriter out = resp.getWriter();
        out.write(json.toString());
        out.flush();
        out.close();


//    ;
//
//        boolean existsName = ud.selectName(na);
//        try {
//            User user = ud.selectByName(na);
//        } catch (Exception e) {
//            e.printStackTrace();
//        }
//
//
//        Map<String, Object> map = new HashMap<>();
//        map.put("existsName",existsName);
//        JSONObject json = JSONObject.fromObject(map);
//
//        boolean existsName1 = json.getBoolean("existsName");
//
//
////        JsonObject json = new JsonObject();
//
//
//        PrintWriter out = resp.getWriter();
//        out.write(String.valueOf(existsName1));


    }
}
